package com.zzuli.yxypre.controller;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzuli.yxypre.comment.CustomException;
import com.zzuli.yxypre.comment.R;
import com.zzuli.yxypre.entity.Album;
import com.zzuli.yxypre.entity.Comment;
import com.zzuli.yxypre.entity.Sheet;
import com.zzuli.yxypre.service.SheetService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @auther pony
 * @create 2023-05-09 13:41
 */
@RestController
@RequestMapping("sheet")
@CrossOrigin(origins = "*")
public class SheetController {
    @Autowired
    private SheetService sheetService;

    /**
     * 根据 歌单 id 获取一张 歌单信息
     * @param id
     * @return
     */
    @GetMapping("getOneById/{id}")
    public R getOneById(@PathVariable("id") Integer id, HttpServletRequest request){
        return sheetService.getOneById(id, request);
    }

    /**
     * 获取歌单的分页评论数据
     * @param order 排序关键字
     * @param sheetId 歌单id
     * @param pageCurrent 当前页
     * @param pageSize 每页大小
     * @return
     */
    @GetMapping("/commentPage/{order}/{sheetId}/{pageCurrent}/{pageSize}")
    public R commentPage(@PathVariable("order") String order, @PathVariable("sheetId") Integer sheetId, @PathVariable("pageCurrent") Integer pageCurrent,
                         @PathVariable("pageSize") Integer pageSize, HttpServletRequest request){
        System.out.println(sheetId);
        System.out.println(pageCurrent);
        System.out.println(pageSize);
        return sheetService.commentPage(order, sheetId, pageCurrent, pageSize, request);
    }


    /**
     * 为歌单，添加评论
     * @param comment
     * @param request
     * @return
     */
    @PostMapping("/addComment")
    public R addComment(@RequestBody Comment comment, HttpServletRequest request){
        System.out.println(comment);
        // 判断评论内容是否为空
        if ("".equals(comment.getContent())||comment.getContent()==null){
            return null;
        }
        return sheetService.addComment(comment, request);
    }

    /**
     * 为歌单评论点赞
     * @param commentId 评论id
     * @param flag 1：点赞 0：取消点赞
     * @param request
     * @return
     */
    @PutMapping("/thumbs/{commentId}/{flag}")
    public R thumbs(@PathVariable("commentId") Integer commentId, @PathVariable("flag") Integer flag,  HttpServletRequest request){
        return sheetService.thumbs(commentId, flag, request);
    }

    /**
     * 修改歌单 type 标签
     * @param id
     * @param tagList
     * @return
     */
    @PutMapping("/addType/{id}")
    public R addType(@PathVariable("id") Integer id, @RequestBody String tagList){
        // {"tagList":["pony","pony1"]} 转化为数组
        String substring = tagList.substring(tagList.indexOf("["), tagList.indexOf("]")+1);
        System.out.println(substring);
        ObjectMapper objectMapper = new ObjectMapper();
        String[] tagArr = new String[0];
        try {
            tagArr = objectMapper.readValue(substring, String[].class);
        } catch (JsonProcessingException e) {
            new CustomException().setMsg("系统错误请联系管理员");
        }
        return sheetService.addType(id, tagArr);
    }

    /**
     * 添加歌单
     * 歌单图片，请参考下面的各自的上传方法
     * @param sheet
     * @return
     */
    @PostMapping("/addSheet")
    public R addSinger(@RequestBody Sheet sheet,HttpServletRequest request) {
        System.out.println("album:"+sheet);
        // 数据非空判断，主要是 歌曲名和状态
        if (sheet.getName()==null||"".equals(sheet.getName())){
            return R.error().message("歌单添加失败，歌单名不为空！");
        }
        return sheetService.addSheet(sheet,request);
    }

    /**
     * 修改歌单
     * 歌单图片，请参考下面的各自的上传方法
     * @param sheet
     * @return
     */
    @PutMapping("/updateSheet")
    public R updateSheet(@RequestBody Sheet sheet,HttpServletRequest request) {
        System.out.println("album:"+sheet);
        // 数据非空判断，主要是 歌曲名和状态
        if (sheet.getName()==null||"".equals(sheet.getName())){
            return R.error().message("歌单添加失败，歌单名不为空！");
        }
        return sheetService.updateSheet(sheet,request);
    }

    /**
     * 删除歌单
     * 歌单图片，请参考下面的各自的上传方法
     * @param sheetId
     * @return
     */
    @DeleteMapping("/deleteSheet/{sheetId}")
    public R deleteSheet(@PathVariable("sheetId") Integer sheetId, HttpServletRequest request) {
        return sheetService.deleteSheet(sheetId,request);
    }

    /**
     * 添加歌单-上传歌单图片
     * @param file 图片
     * @param id 要添加图片的专辑的id
     * @return
     */
    @PostMapping("/uploadAddImg")
    public R uploadAddImg(MultipartFile file) {
        // System.out.println("fileName:"+file.getOriginalFilename());
        // 1、文件校验
        // 是否是图片文件 image/jpeg，是不是以 image 开头的
        if (!file.getContentType().startsWith("image")){
            return R.error().message("上传的文件不是图片类型");
        }
        if (file.getSize() > 30*1024*1024){
            return R.error().message("上传的图片过大，超过30MB");
        }
        return sheetService.uploadAddImg(file);
    }

    /**
     * 为专辑添加歌曲
     * @param sheetId
     * @param request
     * @return
     */
    @PostMapping("/addSong/{sheetId}/{songId}")
    public R addSong(@PathVariable("sheetId") Integer sheetId,
                          @PathVariable("songId") Integer songId,
                          HttpServletRequest request) {
        return sheetService.addSong(sheetId, songId, request);
    }
}
